<?php

class IndexAction extends BaseAction {

  /*  进入用户中心的界面有三种情况
   *  第一，/main/user.php，进入默认首页，既系个人中心首页
   *  第二，/main/user.php/pwurl/{$url}，右边的frame对应展现页面
   *  第三，/main/user.php/type/1/menu/1，展现对应点击左边连接的界面
  */
  public function index() {
    //$_SESSION['tp_user_center_login'] = 1;
    $urls = array(
      1=>array(name=>"我的帐户",urls=>array(
          1=>array(name=>"个人资料",url=>"/pw8/profile.php?action=modify"),2=>array(name=>"隐私设置",url=>""))
      ),
      2=>array(name=>"我的专栏",urls=>array(
          1=>array(name=>"专栏首页",url=>__ROOT__."/public.php/column/index/uid/".$this->_uid),2=>array(name=>"博文管理",'url'=>"/pw8/apps.php?q=diary"),
          3=>array(name=>"发表博文",url=>"/pw8/apps.php?q=diary&a=write"),4=>array(name=>"我的相册",url=>"/pw8/apps.php?q=photos")
        )),
      3=>array(name=>"我的群组",urls=>array(
          1=>array(name=>"群组管理",url=>"/pw8/apps.php?q=groups&a=all"),2=>array(name=>"群组动态",url=>__URL__."/circleNewArticle"),
          3=>array(name=>"我的群组",url=>"/pw8/apps.php?q=groups&a=my"),4=>array(name=>"创建群组",url=>""),
          5=>array(name=>"站内信息",url=>"/pw8/message.php"),6=>array(name=>"我的好友",url=>"/pw8/u.php?a=friend")
        )),
      4=>array(name=>"资料分享",urls=>array(
          1=>array(name=>"上传资料",url=>__APP__."/doc/upload/uid/".$this->_uid),2=>array(name=>"资料记录",url=>__APP__."/doc/listing/"),
          3=>array(name=>"下载记录",url=>__APP__."/doc/downLoadRecord/"),
        )),
      5=>array(),
      6=>array(),
      7=>array(name=>"财务记录",urls=>array(
          1=>array(name=>"消费记录",url=>__APP__."/doc/expend/uid/".$this->_uid),2=>array(name=>"收益记录",url=>__APP__."/doc/revenue/uid/".$this->_uid),
        )),
      8=>array(name=>"我的供求",urls=>array(
          1=>array(name=>"我的发布",url=>__APP__."/require/publishs/"),
        )),
    );
    //dump($_GET);
	if ($_GET["pwurl"]) $urlDefault =urldecode(str_replace('@','/',$_GET["pwurl"]));
	
    else if ($_GET["type"] && $_GET["menu"]) $urlDefault = $urls[$_GET["type"]]["urls"][$_GET["menu"]]["url"];
    else $urlDefault = __URL__."/userDefault";
    //获取用户姓名
    $m = M();
    $name = $m->query("select username from pw_members where uid = ".$this->_uid);
    $this->assign("username", $name[0]["username"]);
    $this->assign("urls", $urls);
    $this->assign("dev", $this->_dev);
    $this->assign("url_default", $urlDefault);
    $this->display();
  }

  public function userDefault() {
    //统计信息
    $total = $this->_getTotalInfo($this->_uid);
    $this->assign("total", $total);
    //个人动态
    $newest = $this->_getUserNewest($this->_uid);
    $this->assign("newest", $newest);
    //推荐文章
    $recommend = $this->_getRecommendArticles();
    $this->assign("recommend_list",$recommend);
    $this->display('user-default');
  }

  /* 统计信息
   * 上次登录时间：lastvisit 　 上次登录IP：ip
   *  加入群组： cy_num 个      好友数量： f_num 个
   *  发表文章： a_num 篇         被访次数： visits 次
  */
  public function _getTotalInfo($uid) {
    $m = M();
    $sql = "select t1.uid,t1.username,t2.lastvisit,t2.onlineip,t2.f_num,ifnull(t3.visits,0) as visits
      from pw_members t1
      left join pw_memberdata t2 on (t1.uid = t2.uid)
      left join pw_space t3 on (t1.uid = t3.uid)
      where t1.uid = $uid";
    $result = $m->query($sql);
    $rt = $result[0];
    $rt["lastvisit"] = date("Y-m-d H:i:s",$rt["lastvisit"]);
    $ip = explode('|',$rt["onlineip"]);
    $ip = explode(" ", $ip[0]);
    $rt["ip"] = $ip[0];
    $result = $m->query("select count(*) from pw_cmembers where uid = $uid");
    $rt["cy_num"] = $result[0]["count(*)"];
    $a1 = $m->query("select count(*) from pw_diary where uid = $uid");
    $a2 = $m->query("select count(*) from a_pw_colonys_article where uid = $uid");
    $rt["a_num"] = $a1[0]['count(*)'] + $a2[0]['count(*)'];
    return $rt;
  }

  //用户最新动态，包括个人博文和群组文章(type=1/type-2)
  private function _getUserNewest($uid) {
    $m = M();
    $sql = "select did as id, uid, subject as title, from_unixtime(postdate) as postdate from pw_diary where uid = $uid order by postdate desc limit 0,3";
    $dirays = $m->query($sql);
    foreach ($dirays as $key => $value) {
      $dirays[$key]["type"] = 1;
      $dirays[$key]["url"] = "/pw8/apps.php?q=diary&a=detail&did=".$value["id"];//博文的连接，跳到对应的个人专栏里面的个人博文
    }
    $sql = "select id, uid, cyid, title, create_time as postdate from a_pw_colonys_article where uid = $uid order by postdate desc limit 0,3";
    $articles = $m->query($sql);
    foreach ($articles as $key => $value) {
      $articles[$key]['type'] = 2;
      $articles[$key]["url"] = '/main/public.php?s=/circle/article/cyid/'.$value['cyid'].'/id/'.$value['id'];
    }
    $list = array_merge($articles,$dirays);
    $list = $this->_sort($list,"postdate");
    return $list;
  }

  //数组排序，按照日期降序排序
  private function _sort($list,$key) {
    $len = count($list);
    $temp = "";
    for ($i = 0; $i < $len; $i++) {
      for ($j = $i+1; $j < $len; $j++) {
        if ($list[$j][$key] > $list[$i][$key]) {
          $temp = $list[$i];
          $list[$i] = $list[$j];
          $list[$j] = $temp;
        }
      }
    }
    return $list;
  }

  private function _getRecommendArticles() {
    $m = M('ARecommendPart');
    $part = $m->field('id')->where("name='社区_推荐文章'")->find();
    $sql = "select type_id  from a_recommend where type = ".$part["id"]." order by sort limit 0,6";
    $idList = $m->query($sql);
    $ids = array();
    foreach ($idList as $k => $v) $ids[] = $v["type_id"];
    $sql = 'select * from a_pw_colonys_article where id in ('.implode(',', $ids).')';
    $list = $m->query($sql);
    return $list;
  }

  /*  我的群组--最近话题
   *  选出用户所有群组中最新的20条话题
  /*/
  public function circleNewArticle() {
    $m = M();
    $sqlUids = "select uid from pw_cmembers where colonyid in (select colonyid from pw_cmembers where uid = ".$this->_uid.") group by uid";
    $sql = "select t1.*,t2.cname from a_pw_colonys_article t1  left join pw_colonys t2 on  t1.cyid=t2.id  where uid in ($sqlUids)order by t1.CREATE_TIME desc limit 20";
    $list = $m->query($sql);
    $this->assign('list', $list);
    $this->display("recent-topics");
  }


}
?>
